

// import request from '../../request'
import * as constant from '../../constant'
import comment from '../../request/comment'

const initState = {
    // id:'',
    body: '',
    errors: null,
    comments: [],


}

const commentReduer = (state = initState, action) => {

    switch (action.type) {
        case constant.COMMENT_UPDATE_FIELD:
            const key = action.key
            const value = action.value
            return { ...state, [key]: value }
        case constant.COMMENT_CREATE_RESULT:
            if (action.result.status === 1) {
                const comment = action.result.data//创建评论 需要合并
                console.log('reducer', comment);
                // comments.push(comment)
                return { ...state, body: '', comments: state.comments.concat([comment]) }
            } else {
                return { ...state, body: '', errors: { message: action.result.message } }
            }
        case constant.COMMENT_GET_RESULT:
            if (action.result.status === 1) {
                const comments = action.result.data  //第一次获取不需要合并
                // console.log('reducer', comments);
                // comments.push(comment)
                return { ...state, body: '', comments }
            } else {
                return { ...state, body: '', errors: { message: action.result.message } }
            }
        case constant.COMMENT_DELETE_RESULT:
            if (action.result.status === 1) {
                const commentId = action.result.id
                return {...state, body: "", comments: state.comments.filter(comment => {
                        return comment.id != commentId
                    })
                }
            } else {
                return { ...state, body: '', errors: { message: action.result.message } }
            }
        default:
            break;
    }
    return state

}


export default commentReduer